package rlink

import (
	"context"
	"fmt"
	"gitee.com/ruige_fun/util/rpwd"
	"github.com/redis/go-redis/v9"
)

// LinkRedis 连接Redis数据库，返回 *redis.Client
func LinkRedis(dbNumber uint, accPassword rpwd.AccountAndPassword) (*redis.Client, error) {
	client := redis.NewClient(&redis.Options{
		Addr:       fmt.Sprint(accPassword.IP, ":", accPassword.Port),
		Username:   accPassword.Account,
		Password:   accPassword.Password,
		DB:         int(dbNumber),
		MaxRetries: 10, //命令最大重试次数
	})
	err := client.Ping(context.Background()).Err()
	if err != nil {
		fmt.Println("连接 Redis 数据库", dbNumber, "失败：", err)
		return nil, err
	}

	fmt.Println("连接 Redis 数据库", dbNumber, "成功！")
	return client, nil
}
